package com.hdw;

import com.google.common.util.concurrent.ThreadFactoryBuilder;
import com.hdw.sayhi.service.impl.SayService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;
import java.util.concurrent.ThreadFactory;


/**
 * @author TuMinglong
 * @description Application
 * @date 2017年9月5日下午8:55:08
 */
@SpringBootApplication
public class BaseServiceApplication extends SpringBootServletInitializer {

    protected final static Logger logger = LoggerFactory.getLogger(BaseServiceApplication.class);

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        logger.info("----BaseServiceApplication 启动----");
        return application.sources(BaseServiceApplication.class);

    }

    public static void main(String[] args) {
        SpringApplication.run(BaseServiceApplication.class, args);
        logger.info("----BaseServiceApplication 启动----");
        SayService.MyCallable myCallable = new SayService().new MyCallable(1);
    }


    @Bean("taskExecutor")
    public Executor taskExecutor(){
        ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
        taskExecutor.setCorePoolSize(100);
        taskExecutor.setMaxPoolSize(500);
        taskExecutor.setQueueCapacity(1000);
        taskExecutor.setKeepAliveSeconds(60);

        taskExecutor.setWaitForTasksToCompleteOnShutdown(true);
        taskExecutor.setAwaitTerminationSeconds(60);
        ThreadFactory build = new ThreadFactoryBuilder()
                .setNameFormat("taskExecutor--")
                .setUncaughtExceptionHandler(((t, e) -> logger.error("ThreadPool:{}  Exception: {}",t,e))).build();

        taskExecutor.setThreadFactory(build);
        return taskExecutor;
    }
}
